package com.zzq.guli.service.ucenter.controller;


import com.zzq.guli.service.base.helper.JwtHelper;
import com.zzq.guli.service.base.helper.JwtInfo;
import com.zzq.guli.service.base.result.R;
import com.zzq.guli.service.base.result.ResultCodeEnum;
import com.zzq.guli.service.base.util.FormUtils;
import com.zzq.guli.service.ucenter.entity.LoginForm;
import com.zzq.guli.service.ucenter.entity.RegisterForm;
import com.zzq.guli.service.ucenter.entity.User;
import com.zzq.guli.service.ucenter.service.MemberService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.joda.time.format.FormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ZZQ
 * @since 2024-05-16
 */

//@CrossOrigin
@RestController
@RequestMapping("api/user/member")
public class MemberController {



    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    MemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(
            @ApiParam(value = "登录表单", required = true)
            @RequestBody LoginForm loginForm){
        String mobile=loginForm.getMobile();
        String password=loginForm.getPassword();

        if(StringUtils.isEmpty(mobile)&&StringUtils.isEmpty(password)&&!FormUtils.isMobile(mobile)){
            return R.setResult(ResultCodeEnum.PARAM_ERROR);
        }

        String token = memberService.login(loginForm);
        return R.ok().data("token", token).message("登录成功");
    }

    @ApiOperation(value = "注册")
    @PostMapping("register")
    public R login(
            @ApiParam(value = "注册表单", required = true)
            @RequestBody RegisterForm registerForm){
        String mobile=registerForm.getMobile();
        String password=registerForm.getPassword();
        String nickname=registerForm.getNickname();
        String code=registerForm.getCode();

        ///校验参数
        if (StringUtils.isEmpty(mobile)
                || !FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(nickname)
        ) {
            return R.setResult(ResultCodeEnum.PARAM_ERROR);
        }

        //验证验证码是否错误
        String key="checkCode::"+mobile;
        String o = (String) redisTemplate.opsForValue().get(key);
        if(!o.equals(code)){
            return R.setResult(ResultCodeEnum.CHECKCODE_ERROR);
        }

        memberService.register(registerForm);

        return R.ok().message("注册成功");
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/get-login-info")
    public R getLoginInfo(HttpServletRequest request){

        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("userInfo", jwtInfo);
    }
}

